package cn.fage.servlet;

import cn.fage.po.CharacterInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.tomcat.util.http.fileupload.IOUtils;

/**
 * 另外，url的配置也可以采用配置文件的方式，即
 * <p>
 * 去掉servlet类的注解中的路径，只保留“@WebServlet”这部分
 * 然后在web.xml文件中加上如下配置：
 * <servlet>
 * <description></description>
 * <display-name>FindCharacterInfoServlet</display-name>
 * <servlet-name>FindCharacterInfoServlet</servlet-name>
 * <servlet-class>com.twf.demo.servlet.FindCharacterInfoServlet</servlet-class>
 * </servlet>
 * <servlet-mapping>
 * <servlet-name>FindCharacterInfoServlet</servlet-name>
 * <url-pattern>/FindCharacterInfoServlet</url-pattern>
 * </servlet-mapping>
 */

/**
 * @author lin
 * @version 1.0
 * @date 2020-07-29 11:03
 * @Description TODO
 */
@WebServlet("/FindCharacterInfoServlet")  // 该注解将会作为接口url的一部分
public class FindCharacterInfoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 响应参数格式设置
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.setStatus(HttpServletResponse.SC_OK);

        // 如果是从JSP页面发起的请求，则参数是以下面的方式获取的。
//		String parameter = request.getParameter("xxx");

        // 现在是以这种方式先获取请求参数得json格式字符串
//        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
//        String body = reader.lines().collect(Collectors.joining());
        // 获取请求参数的json格式字符串

        Map body = request.getParameterMap();


        // 然后将json字符串转为Java对象，这里只对参数做一个打印操作，有需要的朋友可以自行做其做其他操作
//        CharacterInfo characterInfo = JSON.parseObject(body, new TypeReference<CharacterInfo>() {
//        });

        System.out.println(JSON.toJSONString(body));

        // 造几条数据返回
        List<CharacterInfo> list = new ArrayList<CharacterInfo>();
        CharacterInfo characterInfo1 = new CharacterInfo("孙少平", "男", "煤矿工人", "大牙湾煤矿");
        CharacterInfo characterInfo2 = new CharacterInfo("孙少安", "男", "农民企业家", "双水村");
        CharacterInfo characterInfo3 = new CharacterInfo("田晓霞", "女", "记者", "黄原省城");
        CharacterInfo characterInfo4 = new CharacterInfo("田润叶", "女", "教师", "学校");
        list.add(characterInfo1);
        list.add(characterInfo2);
        list.add(characterInfo3);
        list.add(characterInfo4);
        response.getWriter().println(JSON.toJSONString(list));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doDelete(req, resp);
    }
}
